package cn.wolfcode.wms.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.wms.domain.StockOutcomeBill;
import cn.wolfcode.wms.query.StockOutcomeBillQueryObject;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.IDepotService;
import cn.wolfcode.wms.service.IStockOutcomeBillService;
import cn.wolfcode.wms.util.JsonResult;
import cn.wolfcode.wms.util.LogicRuntimeException;
import cn.wolfcode.wms.util.RequiredPermission;

@Controller
@RequestMapping("stockOutcomeBill")
public class StockOutcomeBillController {

	@Autowired
	private IStockOutcomeBillService service;
	@Autowired
	private IDepotService depotService;
	@Autowired
	private IClientService clientService;
	
	@RequestMapping("list")
	@RequiredPermission("出库单列表")
	public String list(Model model,@ModelAttribute("qo")StockOutcomeBillQueryObject qo){
		model.addAttribute("depots", depotService.selectAll());
		model.addAttribute("clients", clientService.selectAll());
		model.addAttribute("result", service.query(qo));
		
		return "stockOutcomeBill/list";
	}
	
	@RequestMapping("delete")
	@RequiredPermission("出库单删除")
	@ResponseBody
	public JsonResult delete(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.deleteByPrimaryKey(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("删除失败");
		}
		return result;
	}
	
	@RequestMapping("input")
	@RequiredPermission("出库单编辑")
	public String input(Model model,Long id){
		model.addAttribute("depots", depotService.selectAll());
		model.addAttribute("clients", clientService.selectAll());
		if (id!=null) {
			model.addAttribute("stockOutcomeBill", service.selectByPrimaryKey(id));
		}
		return "stockOutcomeBill/input";
	}
	
	@RequestMapping("saveOrUpdate")
	@RequiredPermission("出库单保存或更新")
	@ResponseBody
	public JsonResult saveOrUpdate(StockOutcomeBill stockOutcomeBill){
		JsonResult jsonResult = new JsonResult();
		try {
			if (stockOutcomeBill.getId()!=null) {
				service.updateByPrimaryKey(stockOutcomeBill);
			}else {
				service.insert(stockOutcomeBill);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult.markMsg("保存失败");
		}
		return jsonResult;
	}
	
	@RequestMapping("audit")
	@RequiredPermission("出库单审核")
	@ResponseBody
	public JsonResult audit(Long id){
		JsonResult result = new JsonResult();
		try {
			if (id!=null) {
				service.audit(id);
			}
		} catch (LogicRuntimeException e) {
			e.printStackTrace();
			result.markMsg(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			result.markMsg("审核失败");
		}
		return result;
	}
	
	//查看
	@RequestMapping("show")
	@RequiredPermission("查看出库单详情")
	public String show(Model model,Long id){
		model.addAttribute("stockOutcomeBill", service.selectByPrimaryKey(id));
		return "stockOutcomeBill/show";
	}
	
	
}
